var url = serverPre + "/manager/scs/v2/seal/channel/search";

var bsConf = {

    queryFormId: "sc-query-form",

    createFormId: "sc-create-form",

    tableID: "id",

    queryCols: 3,

    queryFloat: "right",

    voName: "sc",
    initData: true,
    // needChangeTab: true,
    //
    // needInitECharts: true,

    queryConfigs:                   //页面头部查询form
        [
            /* {
                 type: 'text',
                 placeholder: '印章名称',
                 label: '印章名称',
                 name: 'sealName',
                 default: '',
                 verify: ''
             },*/
            {
                type: 'select',
                placeholder: '印章状态',
                label: '印章状态',
                name: 'status',
                cols: [["", ""], [1, '启用'], [0, '停用']]
            },
            {
                type: 'text',
                placeholder: '企业名称',
                label: '企业名称',
                name: 'sealOrg',
                default: '',
                verify: ''
            },
            {
                type: 'select',
                id: 'zh',
                placeholder: '租户',
                label: '租户',
                name: 'companyId',
                idProperty: "companyId",
                verify: "companyId_zh",
                dataUrl: serverPre + "/manager/scs/v2/company",
                nameProperty: 'companyAbbrName'
            },
            {
                type: 'select',
                placeholder: '产品',
                label: '产品',
                id: "cp",
                name: 'code',
                idProperty: "code",
                dataUrl: serverPre + "/manager/scs/v2/product",
                nameProperty: 'productName'
            },
            {
                type: 'select',
                placeholder: '管理员',
                label: '管理员',
                name: 'userId',
                idProperty: "userId",
                dataUrl: serverPre + "/manager/scs/v2/product/user",
                nameProperty: 'userName'
            }
        ],

    //列表内容
    tableCols: [
        {field: 'id', title: '序号', type: 'numbers'}
        , {field: 'tenant', width: 185, title: '租户', align: 'center'}
        , {field: 'channelName', width: 100, title: '产品', align: 'center'}
        , {field: 'sealPerson', width: 120, title: '管理员', align: 'center'}
        , {field: 'sealCode', width: 155, title: '印章编号', align: 'center'}
        /* , {field: 'sealName', width: 100, title: '印章名称', align: 'center'}*/
        , {field: 'status', width: 110, title: '印章状态', align: 'center'}
        , {field: 'encryptKey', width: 170, title: '秘钥', align: 'center'}
        /*   , {field: 'sealPwd', width: 120, title: '印章密码', align: 'center'}*/
        , {field: 'createTime', width: 170, title: '创建时间', align: 'center'}
        , {field: 'sealOrg', title: '企业名称', align: 'center'}
        , {fixed: 'right', width: 250, title: '操作', align: 'left', toolbar: '#bar'}
    ],

    bindOwnTableEventCol: function (layEvent, data) {
        if (layEvent === 'disable') { //上线
            sealStatus(data['id'], 2);
        } else if (layEvent === 'enable') { //下线
            sealStatus(data['id'], 1);
        } else if (layEvent === 'view') {//查看统计
            sealInfo(data['id']);
        } else if (layEvent === 'updateImg') {//查看统计
            updateImg(data['id']);
        }
    }
}



function sealStatus(a, b) {
    if (b === 2) {
        var msg = "确定要停用吗？";

    } else {
        var msg = "确定要启用吗？";
    }
    layer.confirm(msg, {icon: 3, title: '修改状态'}, function (index) {
        var u = serverPre + "/manager/scs/v2/seal/channel/" + a + "/" + b;
        $.ajax({
            type: "GET",
            url: u,
            headers: {"token": getCookie("token")},
            success: function (data) {
                setTimeout('window.location.reload()', 500);
            }
        });
    });
}

function query_search() {
    if ($("#up").hasClass("layui-icon-down")) {
        document.getElementById('up').classList.add("layui-icon-up");
        document.getElementById('up').classList.remove("layui-icon-down");
    } else {
        document.getElementById('up').classList.add("layui-icon-down");
        document.getElementById('up').classList.remove("layui-icon-up");

    }
    $("#product-query-form").toggle()
}

function closethis(a) {
    debugger;
    $(a).closest(".layui-layer-page").remove();
    /*var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(a);*/
}
function sealInfo(id) {
    var u = serverPre + "/manager/scs/v2/seal/channel/sealinfo/" + id;
    var response = $.ajax({
        type: "GET",
        url: u,
        headers: {"token": getCookie("token")},
        async: false,
        success: function (data) {
            debugger;
            //  alert(JSON.stringify(data)
        }
    });

    var json = response.responseJSON;

    if (json.code === 0) {
        layer.open({
            type: 1,
            title: '印章信息',
            shadeClose: true,
            shade: false,
            maxmin: true,
            content: generateHtml(json.data),
            area: ['50%', '50%'],
            success: function () {
                $(".layui-layer-setwin").hide();
            }
        });
    } else {
        layer.msg("内部错误", {
            icon: 0,
            time: 2000
        });
    }
}

function generateHtml(data) {
    return "  " +
        " <div class=\"layui-row\" style=\"padding-top: 20px\">\n" +
        "            <div class=\"layui-col-xs8\">\n" +
        "                <div class=\"look-form-class\">\n" +
        "                    <div class=\"layui-form\" id=\"preViewForm\" >\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印章名称</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealName\"  value='" + data.sealName + "' readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印模编码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealCode\" value='" + data.sealCode + "'  name=\"sealCode\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">印章密码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"sealPwd\" value='" + data.sealPwd + "' name=\"sealPwd\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">证件类型</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" id=\"identificationTypeName\" value='" + data.identificationTypeName + "' name=\"identificationTypeName\" lay-verify=\"title\" readonly=\"readonly\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">证件号码</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" name=\"identificationNo\" value='" + data.identificationNo + "' id=\"identificationNo\" readonly=\"readonly\" lay-verify=\"title\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <label class=\"layui-form-label\">生成时间</label>\n" +
        "                            <div class=\"layui-input-block\">\n" +
        "                                <input type=\"text\" name=\"createTime\" id=\"createTime\"  value='" + data.formatCreateTime + "' readonly=\"readonly\" lay-verify=\"title\" style=\"width: 300px\" autocomplete=\"off\" placeholder=\"\" class=\"layui-input\">\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                        <div class=\"layui-form-item\">\n" +
        "                            <div class=\"layui-input-block\" style=\"padding-top:20px;padding-left: 150px\">\n" +
        "                                <button class=\"layui-btn \" onclick='closethis(this)'  id=\"closeWin\">关闭</button>\n" +
        "                            </div>\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "            <div class=\"layui-col-xs3\">\n" +
        "                <div class=\"look-image-form-class\">\n" +
        "                        <div style=\"padding-top: 100px;padding-left: 25px\">\n" +
        "                            <img src=\"data:image/jpeg;base64," + data.image + "\" style=\"width:157px;height:157px\">\n" +
        "                        </div>\n" +
        "                </div>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>"
}

/**
 * 更新公章图片
 */
function updateImg(id) {
    xadmin.open("更新公章图片", "./edit.html?id="+id, 480, 550);
}

/**
 * 文件上传
 */
function uploadFile(upload, uploadUrl) {
    upload.render({
        elem: '#uploadImage'
        , url: uploadUrl
        , accept: 'images' //普通文件
        , headers: {"token": getCookie("token")}
        , size: '1024'
        , exts: "png"
        , done: function (res) {
            if (res.code === 0) {
                $('#pictureId').val(res.data.pictureId);
                $("#fileName").html(res.data.fileName)
                alert("上传成功！");
            } else {
                errorMsg("文件上传失败！")
            }
        }
    });
}

function buildEditWin(form,id) {
    let url = serverPre + "/manager/scs/v2/seal/channel/sealInfo/"+id;
    $.ajax({
        url: url,
        type: 'GET',
        contentType: 'application/json',
        headers: {'token': getCookie('token')},
        success: function (res) {
            if (res.code === 0) {
                var obj = res.data.vo;
                $("#sealOrg").val(obj.sealOrg);
                $("#identificationType").val(obj.identificationType);
                $("#identificationTypeName").val(obj.identificationTypeName);
                $("#identificationNo").val(obj.identificationNo);
                $("#sealCode").val(obj.sealCode);
                $("#sealName").val(obj.sealName);
                $("#keyWords").val(obj.keyWords);
                $("#imageCode").val(obj.ext1);
                $("#imageSize").val(obj.ext2);
                $("#sealPwd").val(obj.sealPwd);
            }
        },
        error: function (err) {
            errorMsg(err);
        }
    });
}

/**
 * 提交表单
 * @param formData
 * @param url
 */
function submitForm(commitData, url) {
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form;
        //监听提交
        form.on('submit(submitBtn)', function (data) {
            if (data.field.pictureId === "") {
                alert("公章图片不能为空!");
                return false;
            }
            saveSealInfo(url, commitData);
            return false;
        });
    })
}

/**
 * 保存印章
 * @param url
 * @param commitData
 */
function saveSealInfo(url, commitData) {
    let loading = layer.msg('正在保存...', {icon: 16, shade: 0.3, time: 0});
    $.ajax({
        url: url,
        data: commitData,
        type: 'POST',
        contentType: 'application/json',
        headers: {'token': getCookie('token')},
        success: function (res) {
            debugger
            if (res.code === 0) {
                layer.close(loading);
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                layer.close(index);
                parent.location.reload();
            } else {
                layer.close(loading);
                alert(res.msg);
            }
        },
        error: function (err) {
            layer.close(loading);
            errorMsg(err);
        }
    });
}
